home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / dance / classes / foot.jav < prev    next >
Encoding:
Text File  |  1995-10-11  |  6.6 KB  |  263 lines

  1. /*-
  2.  * Copyright (c) 1995 by Georg Hessmann.
  3.  * All Right Reserved.
  4.  *
  5.  * Foot.java    1.0   25 Aug 1995
  6.  *              1.1   13 Sep 1995
  7.  *
  8.  */
  9.  
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.image.*;
  13.  
  14. import Floor;
  15. import Step;
  16. import Dance;
  17.  
  18. /**
  19.  * Class foot implements the functions for drawing a foot
  20.  * and for getting the size of one foot.
  21.  *
  22.  * @version 1.1, 13 Sep 1995
  23.  * @author Georg Heßmann
  24.  */
  25. class Foot implements ImageObserver {
  26.  
  27.   /** Array der Fuss-Bilder:
  28.    *  Dame ja/nein | Links ja/nein | 8 Richtungen
  29.    */
  30.   static Image FootImages [][][][];
  31.   static int   FootState  [][][][];
  32.  
  33.   static final int IMG_NONE    = 0;    // nothing
  34.   static final int IMG_COMMING = 1;    // some bits are here
  35.   static final int IMG_READY   = 2;    // complete here, but not drawn
  36.   static final int IMG_DRAWN   = 3;    // complete here and drawn
  37.                                         // (one of maybe some feets)
  38.   static final int IMG_OK      = 4;    // complete here and drawn
  39.   
  40.   static final int N    = 1;
  41.   static final int NNO  = 2;
  42.   static final int NO   = 3;
  43.   static final int NOO  = 4;
  44.   static final int O    = 5;
  45.   static final int SOO  = 6;
  46.   static final int SO   = 7;
  47.   static final int SSO  = 8;
  48.   static final int S    = 9;
  49.   static final int SSW  = 10;
  50.   static final int SW   = 11;
  51.   static final int SWW  = 12;
  52.   static final int W    = 13;
  53.   static final int NWW  = 14;
  54.   static final int NW   = 15;
  55.   static final int NNW  = 16;
  56.  
  57.   Dance app;
  58.  
  59.   /**
  60.    * Creates the object Foot. Only one object is needed.
  61.    * @param a pointer to the main applet.
  62.    */
  63.   public Foot(Dance a)
  64.   {
  65.     app = a;
  66.     FootImages = new Image [2][2][2][16];
  67.     FootState  = new int   [2][2][2][16];
  68.  
  69.     for (int i=0; i<2; i++) {
  70.       for (int j=0; j<2; j++) {
  71.     for (int k=0; k<2; k++) {
  72.       for (int l=0; l<16; l++) {
  73.         FootImages[i][j][k][l] = null;
  74.         FootState [i][j][k][l] = IMG_NONE;
  75.       }
  76.     }
  77.       }
  78.     }
  79.   }
  80.  
  81.   public boolean imageUpdate(Image img,
  82.                  int infoflags, int x, int y,
  83.                  int width, int height)
  84.   {
  85.     boolean ret        = true;
  86.     boolean doRepaint  = false;
  87.     int     updateTime = 0;
  88.  
  89.     int i, j, k, l;
  90.     i = j = k = l = 0;
  91.  
  92.     all:
  93.     for (i=0; i<2; i++) {
  94.       for (j=0; j<2; j++) {
  95.     for (k=0; k<2; k++) {
  96.       for (l=0; l<16; l++) {
  97.         if (FootImages[i][j][k][l] == img) {
  98.           break all;
  99.         }
  100.       }
  101.     }
  102.       }
  103.     }
  104.  
  105.     if (l == 16) {
  106.       System.out.println("ERROR: in Foot.imageUpdate()");
  107.     }
  108.  
  109.     FootState[i][j][k][l] = IMG_COMMING;
  110.  
  111.     if ((infoflags & WIDTH) != 0 || (infoflags & HEIGHT) != 0) {
  112.       /* Erst wenn man ein drawImage() versucht werden auch die
  113.        * Bits geladen. Ich mach das aber erst, wenn Breite und Hoehe
  114.        * da sind. Deswegen muss ich schon beim eintrudeln dieser
  115.        * Werte ein repaint() machen (und so das drawImage() erzwingen).
  116.        */
  117.       updateTime = 100;
  118.       doRepaint  = true;
  119.     }
  120.     
  121.     if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
  122.       if (FootState[i][j][k][l] != IMG_OK && FootState[i][j][k][l] != IMG_DRAWN) {
  123.     FootState[i][j][k][l] = IMG_READY;
  124.       }
  125.       doRepaint  = true;
  126.       ret = false;
  127.     }
  128.     else if ((infoflags & SOMEBITS) != 0) {
  129.       doRepaint = true;
  130.       updateTime = 250;
  131.     }
  132.  
  133.     if ((infoflags & ERROR) != 0) {
  134.       FootState[i][j][k][l] = IMG_NONE;
  135.       ret = false;
  136.     }
  137.  
  138.     if (doRepaint) {
  139.       app.repaint(updateTime);
  140.     }
  141.  
  142.     return ret;
  143.   }
  144.  
  145.   /**
  146.    * Draws the foot for step s on floor f within graphics context g.
  147.    * @return Has some foot-state changed to IMG_DRAWN.
  148.    */
  149.   public boolean drawFoot(Graphics g, Floor f, Step s)
  150.   {
  151.     boolean ret = false;    // some state changed to img_drawn
  152.     Image img = getImage(f, s);
  153.  
  154.     if (img != null && img.getWidth(this) != -1 && img.getHeight(this) != -1) {
  155.       int xpos = f.CalcXPos(s.x) - img.getWidth(this)/2;
  156.       int ypos = f.CalcYPos(s.y) - img.getHeight(this)/2;
  157.  
  158.       if (getImageState(f, s) == IMG_READY) {
  159.     setImageState(f, s, IMG_DRAWN);
  160.     ret = true;
  161.       }
  162.       // alles geladen und nun auch neu gezeichnet
  163.  
  164.       g.drawImage(img, xpos, ypos, this);
  165.     }
  166.  
  167.     return ret;
  168.   }
  169.  
  170.   /**
  171.    * Redraw the foot for step s on floor f within graphics context g.
  172.    * Draw the foot only, if the image isn't IMG_OK. If it's IMG_READY,
  173.    * set it to IMG_DRAWN.
  174.    */
  175.   public boolean redrawFoot(Graphics g, Floor f, Step s)
  176.   {
  177.     boolean ret = false;    // some state changed to img_drawn
  178.     int imgState = getImageState(f, s);
  179.     
  180.     if (imgState != IMG_OK) {
  181.       Image img = getImage(f, s);
  182.  
  183.       if (img != null && img.getWidth(this) != -1 && img.getHeight(this) != -1) {
  184.  
  185.         int xpos = f.CalcXPos(s.x) - img.getWidth(this)/2;
  186.         int ypos = f.CalcYPos(s.y) - img.getHeight(this)/2;
  187.  
  188.     if (imgState == IMG_READY) {
  189.       setImageState(f, s, IMG_DRAWN);
  190.       ret = true;
  191.     }
  192.     
  193.     g.drawImage(img, xpos, ypos, this);
  194.       }
  195.     }
  196.  
  197.     return ret;
  198.   }
  199.  
  200.   /**
  201.    * Add the step s into the clipping rect for floor f.
  202.    * @see Floor#addToClipRect
  203.    */
  204.   public void addToClipRect(Floor f, Step s)
  205.   {
  206.     Image img = getImage(f, s);
  207.  
  208.     int xpos = f.CalcXPos(s.x) - img.getWidth(this)/2;
  209.     int ypos = f.CalcYPos(s.y) - img.getHeight(this)/2;
  210.  
  211.     f.addToClipRect(xpos, ypos, xpos+img.getWidth(this), ypos+img.getHeight(this));
  212.   }
  213.   
  214.   private Image getImage(Floor f, Step s)
  215.   {
  216.     Image img;
  217.     
  218.     boolean left   = s.is_left;
  219.     boolean dotted = s.is_dotted;
  220.     int     deg    = s.deg;
  221.  
  222.     img = FootImages[f.IsDame() ? 0 : 1][left ? 0 : 1][dotted ? 0 : 1][deg - 1];
  223.  
  224.     if (img == null) {
  225.       String str = "images/" + (f.IsDame() ? "d" : "h") + "leg-"
  226.     + (left ? "l" : "r") + "-" + deg + (dotted ? "-d" : "") + ".gif";
  227.  
  228.       img = app.getImage(app.getCodeBase(), str);
  229.       FootImages[f.IsDame() ? 0 : 1][left ? 0 : 1][dotted ? 0 : 1][deg - 1] = img;
  230.     }
  231.  
  232.     return img;
  233.   }
  234.   
  235.   private int getImageState(Floor f, Step s)
  236.   {
  237.     boolean left   = s.is_left;
  238.     boolean dotted = s.is_dotted;
  239.     int     deg    = s.deg;
  240.  
  241.     return FootState[f.IsDame() ? 0 : 1][left ? 0 : 1]
  242.                   [dotted ? 0 : 1][deg - 1];
  243.   }
  244.   
  245.   private void setImageState(Floor f, Step s, int state)
  246.   {
  247.     boolean left   = s.is_left;
  248.     boolean dotted = s.is_dotted;
  249.     int     deg    = s.deg;
  250.  
  251.     FootState[f.IsDame() ? 0 : 1][left ? 0 : 1][dotted ? 0 : 1][deg - 1] = state;
  252.   }
  253.   
  254.   /**
  255.    * Returns a String object representing this Foot's value.
  256.    */
  257.   public String toString()
  258.   {
  259.     return getClass().getName() + "[dance=" + app + "]";
  260.   }
  261.  
  262. }
  263.